﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.ReceptionGoods
{	public partial class SupplierReturnLine : Form
	{	private	Util.ActionType actionType;
		private	int supplierId;
		private	int? supplierDocId;
//-------------------------------------------------------------------------------------------------
		public	DIProject.DataSets.waybillsData.SupplierReturnPartiesRow PartyRow{get{return partyRow;}}
		private	DIProject.DataSets.waybillsData.SupplierReturnPartiesRow partyRow = null;
//-------------------------------------------------------------------------------------------------
		public	int? Party
		{	get{return partyRow != null? (int?)partyRow.Id: null;}
			set
			{	if(actionType != Util.ActionType.Insert)
					party = value;
			}
		}
		private	int? party = null;

		public	string Manufacturer
		{	get{return textBoxManufacturer.Text;}
			set
			{	if(actionType != Util.ActionType.Insert)
					textBoxManufacturer.Text = value;
			}
		}

		public	int Qntty
		{	get{return (int)numericUpDownQntty.Value;}
			set
			{	if(actionType != Util.ActionType.Insert)
					numericUpDownQntty.Value = qntty = value;
			}
		}
		private	int qntty = 0;

		public	decimal Price
		{	get{return price;}
			set
			{	if(actionType != Util.ActionType.Insert)
				{	price				= value;
					textBoxPrice.Text	= price.ToString("N2");
				}
			}
		}
		private	decimal price = 0m;

		public	string Note
		{	get{return textBoxNote.Text;}
			set{textBoxNote.Text = value;}
		}
//-------------------------------------------------------------------------------------------------
		private void UpdateControls()
		{	if(partyRow != null)
			{	textBoxName.Text			= partyRow.Name;
				textBoxManufacturer.Text	= partyRow.ManufacturerName;
				price						= partyRow.RetailPrice;
				textBoxPrice.Text			= price.ToString("N2");
				if(Qntty > partyRow.Qntty + qntty)
					numericUpDownQntty.Value= partyRow.Qntty + qntty;
				numericUpDownQntty.Maximum	= partyRow.Qntty + qntty;
				buttonAccept.Enabled		= true;
			}
			else
			{	numericUpDownQntty.Maximum	= int.MaxValue;
				buttonAccept.Enabled		= false;
			}
		}
//-------------------------------------------------------------------------------------------------
		public SupplierReturnLine() : this(Util.ActionType.NoAction, 0, null)
		{
		}
		public SupplierReturnLine(Util.ActionType actionType, int supplierId, int? supplierDocId)
		{	InitializeComponent();
			numericUpDownQntty.Minimum = 1;
			numericUpDownQntty.Maximum = int.MaxValue;
			buttonAccept.Enabled		= false;
			this.actionType		= actionType;
			this.supplierId		= supplierId;
			this.supplierDocId	= supplierDocId;
		}
//-------------------------------------------------------------------------------------------------
		private void SupplierReturnLine_Load(object sender, EventArgs e)
		{	try
			{	if(actionType != DIProject.Util.ActionType.Insert)
				{	Text = Properties.Resources.strUpdateDocLine;
					supplierReturnPartiesTableAdapter.Fill(waybillsData.SupplierReturnParties, supplierId);//, party, true);
					if(waybillsData.SupplierReturnParties.Count > 0)
					{
                        DataRow[] rows = waybillsData.SupplierReturnParties.Select("Id = " + party.Value.ToString());
                        partyRow = rows[0] as DIProject.DataSets.waybillsData.SupplierReturnPartiesRow;// waybillsData.SupplierReturnParties[0];
						UpdateControls();
					}
					buttonParty.Enabled = false;
					ActiveControl = numericUpDownQntty;
				}
				else
					Text = Properties.Resources.strInsertDocLine;
				repriceWriteoffReasonTableAdapter.Fill(dictionary.RepriceWriteoffReason);
			}
			catch(Exception ex)
			{	MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
//-------------------------------------------------------------------------------------------------
		private void buttonParty_Click(object sender, EventArgs e)
		{	SupplierReturnParties partyChoice = new SupplierReturnParties(null, true, supplierId);
			if(partyChoice.ShowDialog(this) == DialogResult.OK)
			{	partyRow = partyChoice.CurrentRow;
				UpdateControls();
			}
		}
//-------------------------------------------------------------------------------------------------
	}
}